package com.savi.myapplication.adapter;

import android.app.Activity;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.savi.myapplication.R;
import com.savi.myapplication.model.FlyCardInfo;

import java.util.List;

public class FlyTaskCardDataLeftAdatper extends RecyclerView.Adapter<FlyTaskCardDataLeftAdatper.DataLeftViewHolder> {

    private List<FlyCardInfo> flyCardInfoList;
    private OnItemClickListener onItemClickListener;
    private Integer selected = -1;
    private Activity activity;
    //是否签名
    private boolean isSigned = false;
    //卡片的UUID，用于签字后进行更新该卡片的签字状态
    private String cardUUid = "";

    public FlyTaskCardDataLeftAdatper(Activity activity, List<FlyCardInfo> flyCardInfoList) {
        this.activity = activity;
        this.flyCardInfoList = flyCardInfoList;
    }


    @NonNull
    @Override
    public DataLeftViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.task_left_view_item, parent, false);

        return new DataLeftViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DataLeftViewHolder holder, int position) {
        FlyCardInfo flyCardInfo = flyCardInfoList.get(position);

        if ((isSigned && cardUUid.equals(flyCardInfo.getCardUUid())) || flyCardInfo.getSigned().equals("1")){
            holder.tvTaskCardName.setText(Html.fromHtml("<font color='#00c600'>"+"☆"+"</font>"+flyCardInfo.getStrCardName()+"<font color='#00c600'>"+"☆"+"</font>",Html.FROM_HTML_MODE_LEGACY));
        }else {
            holder.tvTaskCardName.setText(flyCardInfo.getStrCardName());
        }

        holder.tvTaskCardName.setSelected(true);
        holder.tvTaskCardName.setEllipsize(TextUtils.TruncateAt.MARQUEE);
        if (position == selected) {
            holder.llTaskCard.setBackgroundColor(activity.getResources().getColor(R.color.orange));
        } else {
            holder.llTaskCard.setBackgroundColor(activity.getResources().getColor(R.color.bk_color));
        }

    }

    @Override
    public int getItemCount() {
        return this.flyCardInfoList == null ? 0 : flyCardInfoList.size();
    }

    public class DataLeftViewHolder extends RecyclerView.ViewHolder {


        private final LinearLayout llTaskCard;
        private final TextView tvTaskCardName;

        public DataLeftViewHolder(@NonNull View itemView) {
            super(itemView);
            llTaskCard = (LinearLayout) itemView.findViewById(R.id.ll_task_card);
            tvTaskCardName = (TextView) itemView.findViewById(R.id.tv_taskcard_chart);
            llTaskCard.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(getAdapterPosition());
                }
            });
        }
    }

    public boolean isSigned() {
        return isSigned;
    }

    public void setSigned(boolean signed) {
        isSigned = signed;
    }

    public String getCardUUid() {
        return cardUUid;
    }

    public void setCardUUid(String cardUUid) {
        this.cardUUid = cardUUid;
    }

    public List<FlyCardInfo> getFlyCardInfoList() {
        return flyCardInfoList;
    }

    public void setFlyCardInfoList(List<FlyCardInfo> flyCardInfoList) {
        this.flyCardInfoList = flyCardInfoList;
    }

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public Integer getSelected() {
        return selected;
    }

    public void setSelected(Integer selected) {
        this.selected = selected;
    }

    public interface OnItemClickListener {
        void onItemClick(int positon);
    }

}
